//
//  XZchooseAssessingVC.m
//  Teecloud
//
//  Created by PC on 2018/11/6.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZchooseAssessingVC.h"
#import "XZchooseAssessingCell.h"
#import "XZAcceptUsers.h"
@interface XZchooseAssessingVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectArray;

@end
#define AssessingCell @"AssessingCell"
@implementation XZchooseAssessingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kClearColor;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XZchooseAssessingCell class]) bundle:nil] forCellReuseIdentifier:AssessingCell];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.AcceptUsers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    XZAcceptUsers * model = self.AcceptUsers[indexPath.row];
    cell.textLabel.text = model.Name;
    // 如果当前 cell 的数据在选中数据中，改变样式
    if ([self.selectArray containsObject:model]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *selCell = [tableView cellForRowAtIndexPath:indexPath];
     XZAcceptUsers *model = self.AcceptUsers[indexPath.row];
    // 判断是否选中的 cell
    if (self.selectArray.count != 0) { //  有
        
        // 判断选中数据中是否包含当前的cell的数据
       
        if ([self.selectArray containsObject:model]){ // 有
            
            // 删除当前数据
            [self.selectArray removeObject:model];
            // 将 cell 样式改为未选择样式
            selCell.accessoryType = UITableViewCellAccessoryNone;
            
        }else{ // 没有
            
            // 将当前数据添加到选中数据中
            [self.selectArray addObject:model];
            // cell样式改为选中样式
            selCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        
    }else{ // 没有
        
        // 添加
        [self.selectArray addObject:model];
        // 改样式
        selCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
//遮罩层
- (IBAction)cancleTapAction:(UITapGestureRecognizer *)sender {
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
}

//取消
- (IBAction)cancleButtonAction:(UIButton *)sender {
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
}
//确认
- (IBAction)confirmButtonAction:(UIButton *)sender {
    if (self.selectArray.count > 0) {
        if (self.ChooseAssesseBlock) {
            self.ChooseAssesseBlock(self.selectArray);
        }
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }else{
        [MBProgressHUD showErrorOrSuc:@"请勾选选项" toView:self.view];
    }
}
- (NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray  = [[NSMutableArray alloc]init];
    }
    return _selectArray;
}
@end
